package com.aj.activity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.activity.dialog.ChooseDateDialog;
import com.aj.activity.dialog.ChooseDateDialog.IChooseDay;
import com.aj.activity.dialog.CitysDialog;
import com.aj.activity.dialog.CitysDialog.ISureCityInterface;
import com.aj.activity.dialog.LogoutDialog;
import com.aj.activity.dialog.LogoutDialog.ILogoutInterface;
import com.aj.activity.dialog.SexChooseDialog;
import com.aj.activity.dialog.SexChooseDialog.IChooseSexInterface;
import com.aj.activity.view.CircleImageView;
import com.aj.utils.Contants;

public class UserInfoActivity extends BaseActivity implements
		ISureCityInterface, IChooseDay, IChooseSexInterface, ILogoutInterface {

	private RelativeLayout userphoto_image_linearlayout;
	private RelativeLayout nickname_relativelayout;
	private RelativeLayout name_relativelayout;
	private RelativeLayout sign_relativelayout;
	private RelativeLayout sex_relativelayout;
	private RelativeLayout birthdate_relativelayout;
	private RelativeLayout birtharea_relativelayout;
	private Button exitsbutton;

	private TextView nickname_text;
	private TextView name_text;
	private TextView sex_text;
	private TextView birthdate_text;
	private TextView birtharea_text;
	private TextView sign_text;

	private CitysDialog dialog;
	private ChooseDateDialog chooseDateDialog;
	private SexChooseDialog sexChooseDialog;
	private LogoutDialog logoutDialog;

	protected static final int ITEM_CARTOON = 0;
	protected static final int ITEM_CAMERA = 1;
	protected static final int ITEM_PICTURE = 2;
	protected static final int ITEM_CANCEL = 3;
	private Bitmap portraitBitmap;

	protected static final String[] MENU_NAME_ARRAY = new String[4];

	private ListView menuList;

	private CircleImageView head_img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo);
		initView();
	}

	private void showCityDialog() {
		if (dialog == null) {
			dialog = new CitysDialog(this);
			dialog.setSureCityInterface(this);
		}
		dialog.show();
	}

	private void showChooseDate() {
		if (chooseDateDialog == null) {
			chooseDateDialog = new ChooseDateDialog(this);
			chooseDateDialog.setChooseDay(this);
		}
		chooseDateDialog.show();
	}

	private void showChooseSex() {
		if (sexChooseDialog == null) {
			sexChooseDialog = new SexChooseDialog(this);
			sexChooseDialog.setChooseSexInterface(this);
		}
		sexChooseDialog.show();
	}

	private void initView() {
		userphoto_image_linearlayout = (RelativeLayout) findViewById(R.id.userphoto_image_linearlayout);
		nickname_relativelayout = (RelativeLayout) findViewById(R.id.nickname_relativelayout);
		name_relativelayout = (RelativeLayout) findViewById(R.id.name_relativelayout);
		sign_relativelayout = (RelativeLayout) findViewById(R.id.sign_relativelayout);
		birthdate_relativelayout = (RelativeLayout) findViewById(R.id.birthdate_relativelayout);
		birtharea_relativelayout = (RelativeLayout) findViewById(R.id.birtharea_relativelayout);
		sex_relativelayout = (RelativeLayout) findViewById(R.id.sex_relativelayout);
		exitsbutton = (Button) findViewById(R.id.exitsbutton);
		head_img = (CircleImageView) findViewById(R.id.head_img);
		nickname_text = (TextView) findViewById(R.id.nickname_text);
		name_text = (TextView) findViewById(R.id.name_text);
		sex_text = (TextView) findViewById(R.id.sex_text);
		birthdate_text = (TextView) findViewById(R.id.birthdate_text);
		birtharea_text = (TextView) findViewById(R.id.birtharea_text);
		sign_text = (TextView) findViewById(R.id.sign_text);
		MENU_NAME_ARRAY[0] = getString(R.string.userinfo_systemheader_text);
		MENU_NAME_ARRAY[1] = getString(R.string.userinfo_photograph_text);
		MENU_NAME_ARRAY[2] = getString(R.string.userinfo_gallery_text);
		MENU_NAME_ARRAY[3] = getString(R.string.dialog_cannel_text);

		ClickEvent clickEvent = new ClickEvent();
		createMenu();
		userphoto_image_linearlayout.setOnClickListener(clickEvent);
		birthdate_relativelayout.setOnClickListener(clickEvent);
		nickname_relativelayout.setOnClickListener(clickEvent);
		name_relativelayout.setOnClickListener(clickEvent);
		sign_relativelayout.setOnClickListener(clickEvent);
		birtharea_relativelayout.setOnClickListener(clickEvent);
		sex_relativelayout.setOnClickListener(clickEvent);
		exitsbutton.setOnClickListener(clickEvent);
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(Contants.EDITUSERINFO_ACTIVITY_ACTION);
			switch (v.getId()) {
			case R.id.userphoto_image_linearlayout:
				openPanel();
				break;
			case R.id.nickname_relativelayout:
				intent.putExtra(EditUserInfoActivity.EDITTYPE_STRING,
						EditUserInfoActivity.NICKNAME_INT);
				startActivityForResult(intent,
						EditUserInfoActivity.NICKNAME_INT);
				break;
			case R.id.name_relativelayout:
				intent.putExtra(EditUserInfoActivity.EDITTYPE_STRING,
						EditUserInfoActivity.NAME_INT);
				startActivityForResult(intent, EditUserInfoActivity.NAME_INT);
				break;
			case R.id.sign_relativelayout:
				intent.putExtra(EditUserInfoActivity.EDITTYPE_STRING,
						EditUserInfoActivity.SIGN_INT);
				startActivityForResult(intent, EditUserInfoActivity.SIGN_INT);
				break;
			case R.id.birthdate_relativelayout:
				showChooseDate();
				break;
			case R.id.birtharea_relativelayout:
				showCityDialog();
				break;
			case R.id.sex_relativelayout:
				showChooseSex();
				break;
			case R.id.exitsbutton:
				showLogoutDialog();
				break;
			}
		}
	}

	private void showLogoutDialog() {
		logoutDialog = new LogoutDialog(this);
		logoutDialog.setLogoutInterface(this);
		logoutDialog.show();
	}

	private void createMenu() {
		View view = LayoutInflater.from(UserInfoActivity.this).inflate(
				R.layout.menu_choose_dialog, null);
		menuList = (ListView) view.findViewById(R.id.menulist);
		menuList.setAdapter(getMenuAdapter(MENU_NAME_ARRAY));
		setMenuLayout(menuList);
		menuList.setOnItemClickListener(new ItemClickEvent());
	}

	class ItemClickEvent implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent;
			switch (arg2) {
			case ITEM_CARTOON:
				// startAccountPortraitGridActivity();
				Toast.makeText(UserInfoActivity.this, "点击了系统头像",
						Toast.LENGTH_LONG).show();
				break;
			case ITEM_CAMERA:
				intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1);
				break;
			case ITEM_PICTURE:
				intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				intent.putExtra("crop", "true");
				intent.putExtra("aspectX", 1);
				intent.putExtra("aspectY", 1);
				intent.putExtra("outputX", 80);
				intent.putExtra("outputY", 80);
				intent.putExtra("return-data", true);
				startActivityForResult(intent, 2);
				break;
			case ITEM_CANCEL:
				break;
			}
			closePanel();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Toast.makeText(this, resultCode + "", Toast.LENGTH_LONG).show();
		String resultString = null;
		if (data != null) {
			resultString = data
					.getStringExtra(EditUserInfoActivity.RESULT_STRING);
		}
		switch (resultCode) {
		case RESULT_OK:
			// System.out.println(data.getExtras().getString("portrait_cartoon"));
			if (data.getExtras() != null) {
				if ((data.getExtras().get("portrait_cartoon") != null)
						&& (requestCode == 0)) {
					cartoonResult(data);
					// new_account_portrait.setImageResource(
					// Integer.valueOf(data.getExtras().get("portrait_cartoon").toString())
					// );
				} else if ((data.getExtras().get("data") != null)
						&& (requestCode == 1)) {
					cameraResult(data);
				} else if ((data.getExtras().get("data") != null)
						&& (requestCode == 2)) {
					pictureResult(data);
				}
			} else if (data.getData() != null) {
				if (requestCode == 2)
					MIUIpictureResult(data);
			}

			if (portraitBitmap != null) {
				// // portraitBitmap = ImageUtil.ImageCrop(portraitBitmap);
				// // portraitBitmap = ImageUtil.zoomBitmap(portraitBitmap, 200,
				// 200);
				// //
				// // portrait_path = FileHelper.saveBitmap(ConfigInfo.APP_PATH,
				// // System.currentTimeMillis() + "", portraitBitmap);
				// portraitBitmap = ImageUtil.getRoundedCornerBitmap(
				// portraitBitmap, UIUtil.dip2px(this, 100));
				head_img.setImageBitmap(portraitBitmap);
				// portraitBitmap =
				// UIUtil.centerSquareScaleBitmap(portraitBitmap, 200);
				// portraitBitmap =
				// ImageUtil.getRoundedCornerBitmap(portraitBitmap,
				// UIUtil.dip2px(this, 50));
				// portraitBitmap =
				// UIUtil.getRoundedCornerBitmap(portraitBitmap, 12);
				// portrait.setImageBitmap(portraitBitmap);
				// UIUtil.toCorner(new_account_portrait, 20);

			} else
				// portrait_path = null;
				break;
		case EditUserInfoActivity.NICKNAME_INT:
			if (resultString != null) {
				nickname_text.setText(resultString);
			}
			break;
		case EditUserInfoActivity.NAME_INT:
			if (resultString != null) {
				name_text.setText(resultString);
			}
			break;
		case EditUserInfoActivity.SIGN_INT:
			if (resultString != null) {
				sign_text.setText(resultString);
			}
			break;
		default:
			break;
		}

	}

	// 缺图像裁剪压缩
	protected void cameraResult(Intent data) {
		portraitBitmap = (Bitmap) data.getExtras().get("data");// 获取相机返回的数据，并转换为Bitmap图片格式
	}

	// 缺图像裁剪压缩
	protected void pictureResult(Intent data) {
		portraitBitmap = (Bitmap) data.getExtras().get("data");
	}

	// 缺图像裁剪压缩
	protected void MIUIpictureResult(Intent data) {
		Uri originalUri = data.getData();
		if (originalUri != null) {
			try {
				portraitBitmap = MediaStore.Images.Media.getBitmap(
						this.getContentResolver(), originalUri);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	protected void cartoonResult(Intent data) {
		Resources res = getResources();
		portraitBitmap = BitmapFactory.decodeResource(
				res,
				Integer.valueOf(data.getExtras().get("portrait_cartoon")
						.toString()));
	}

	protected SimpleAdapter getMenuAdapter(String[] menuNameArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.item_menu_text, new String[] { "itemText" },
				new int[] { R.id.item_text });
		return simperAdapter;
	}

	@Override
	public void sureCity(String s) {
		birtharea_text.setText(s);
	}

	@Override
	public void chooseDaySuccess(int year, int mouth, int day) {
		birthdate_text.setText((year + "-" + mouth + "-" + day));
	}

	@Override
	public void chooseSex(String sex) {
		sex_text.setText(sex);
	}

	@Override
	public void sureLogout() {
		finish();
	}
}
